<template>
  <div>
    <el-tooltip effect='light'>
      <div slot="content">点击添加试卷</div>
      <el-button type="primary" class="el-icon-plus addbtn" @click="dialogFormVisible = true" size="small">添加试卷</el-button>
    </el-tooltip>
    <el-dialog title="添加试卷" :visible.sync="dialogFormVisible" center :show-close="false" :close-on-click-modal="false">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="试卷名称" :label-width="formLabelWidth" prop="test_name">
          <el-input v-model="form.test_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="出卷人" :label-width="formLabelWidth" prop="test_founder">
          <el-input v-model="form.test_founder" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="form.test_description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'Bomb',
    data() {
      return {
        dialogFormVisible: false,
        form: {
          test_name: '',
          test_founder: '',
          test_description: ''
        },
        rules: {
          test_name: [{
            required: true,
            message: '请输入试卷名称',
            trigger: 'blur'
          }],
          test_founder: [{
            required: true,
            message: '请输入出卷人',
            trigger: 'blur'
          }, {
            min: 2,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: 'blur'
          }]
        },
        formLabelWidth: '100px'
      }
    },
    methods: {
      submitForm(form) {
        const examdata  = {'testDescription':this.form.test_description,'testFounder':this.form.test_founder,'testName':this.form.test_name};
        this.$refs[form].validate((valid) => {
          // console.log(123);
          if (valid) {
            this.$postRequest({
                url: '/testPaper/addTest',
                params: examdata
              }).then(res => {
                // console.log(res);
                if (res.code === 200) {
                  console.log(res.data);
                  this.$message({
                    type: 'success',
                    message: '试卷创建成功！！'
                  });
                  this.$emit('getnewexam',1);
                  // _this.$store.commit('login', data)
                  // var path = _this.$route.query.redirect
                  // _this.$router.replace({
                  //   // path: path === '/' || path === undefined ? '' : path
                  //   path: 'index'
                  // });
                } else {
                  this.$alert(res.msg[0], '提示', {
                    confirmButtonText: '确定'
                  })
                }
              })
              .catch(failResponse => {});

            this.dialogFormVisible = false;
          } else {
            // console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style>
  .addbtn {
    color: #FFFFFF;
    height: 100%;
    background-color: #5a9cff;

  }
</style>
